package app.TouTiao;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import qiurank.PaFather;
import qiurank.UrlManageInterface;
import qiurank.UrlObject;

/**
 * Created by Administrator on 2016/1/4.
 */
public class TouTiaoPa extends PaFather {
    public TouTiaoPa(UrlObject urlObject, UrlManageInterface _urlManage){
        super(urlObject,(app.TouTiao.UrlManage)_urlManage);
    }
    /**
     * 获得文章标题
     * @return 文章标题
     */
    public String getContentTitle(){
        //规则
        String pat="<div class=\"title\"><h1>(.*?)</h1>";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);

        if(matcher.find()){
            return matcher.group(1);
        }

        return null;
    }

    /**
     * 获得评论数量
     * @return  评论数量
     */
    public int getCommentNums(){
        //规则
        String pat="<span class=\"cc\">(.*?) 条评论</span>";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);

        if(matcher.find()){
            return Integer.parseInt(matcher.group(1));
        }

        return 0;
    }

    /**
     * 获得发布来源
     * @return 返回发布来源
     */
    public String getFrom(){
        //规则
        String pat="<span class=\"(profile_avatar|src)\">(.*?)</span>";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);

        if(matcher.find()){
            return matcher.group(2).replaceAll("<.*?>", "");
        }

        return null;
    }
    /**
     * 获得抓取页面的文章内容
     * @return  文章内容
     */
    public String getContent(){
        //规则
        String pat="<div class=\"article-content\">(.*?)</div></div><div id=\"pagelet-detailbar\"";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);

        if(matcher.find()){
            return matcher.group(1);
        }

        return null;
    }
    /**
     * 获得抓取文章的创建时间
     * @return 整形的创建时间
     */
    public long getContentCreateTime(){
        //规则
        String pat="<span class=\"time\">(.*?)</span>";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);

        if(matcher.find()){
            String time= matcher.group(1);
            try {
                long itime=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time).getTime();
                return itime;
            } catch (ParseException e) {
                return 0;
            }
        }

        return 0;
    }
    public void getAllJsonUrl(){
        String json=getHtmlContent();
        JSONObject jsonObject=new JSONObject(json);
        JSONArray data=jsonObject.getJSONArray("data");

        for(int i=0;i<data.length();i++){
            JSONObject j=data.getJSONObject(i);
           // Log.console(j.getString("title")+"#####"+j.getLong("create_time")+"#####"+urlobj.getProtocol()+"://"+urlobj.getHost()+j.getString("seo_url"));
            long tmp=j.getLong("create_time");
            saveUrl(urlobj.getProtocol()+"://"+urlobj.getHost()+j.getString("seo_url"),10);
        }



    }

    public String getMaxBehotTime(){
        //规则
        String pat="'max_behot_time': '(.*?)',";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);
        if(matcher.find()){
            return matcher.group(1);
        }
        return null;
    }
    public String getCategory(){
        //规则
        String pat="'category': '(.*?)'}";
        //执行
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);
        if(matcher.find()){
            return matcher.group(1);
        }
        return null;
    }
    /**
     * 获得页面的所有url
     * @return  返回页面的所有超链接
     */
    public void getAllUrl(){
        Filter plan=(Filter)urlManage.plan;
        String max_behot_time=getMaxBehotTime();
        String category=getCategory();
        if(max_behot_time!=null&&category!=null){
            String api_url="http://toutiao.com/api/article/recent/?source=2&count=20&category="+category+"&max_behot_time="+max_behot_time+"&utm_source=toutiao&offset=0&_="+new Date().getTime();
            //Log.console(api_url);
            saveUrl(api_url,9);
        }
        //规则
        String pat="<a .+?>";
        Pattern pattern=Pattern.compile(pat);
        Matcher matcher=pattern.matcher(htmlContent);
        while (matcher.find()){
            String atag=matcher.group();
            //Log.console(atag);
            String href="href=\"(.*?)\"";
            Pattern aTagPattern=Pattern.compile(href);
            Matcher aTagMatcher=aTagPattern.matcher(atag);
            if(aTagMatcher.find()){
                String aurl=aTagMatcher.group(1);
                if(aurl.startsWith("/")||aurl.startsWith("http")){
                    if(aurl.startsWith("/")){
                        aurl=urlobj.getProtocol()+"://"+urlobj.getHost()+aurl;
                    }
                    if(plan.doPlanPaingUrl(aurl)){
                        saveUrl(aurl,11);
                    }else{
                        saveUrl(aurl);
                    }

                    //Log.console(aurl);
                }
            }

        }
    }

}
